using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fated.SaveEngine;

namespace Fated.Character {
    /*
     * Equipment should have statBonus() which returns the stat mod 
     * total of equipped items.
     * AddItem(), RemoveItem()
     * EquipItem, replaces equip slot item
     */
    internal class Equipment : IJSON {
        private Dictionary<string, int> data = new Dictionary<string, int>();
        private enum SlotNames { Head, Chest, RHand, LHand, Legs, Boots }
        private string[] slots = new string[6];

        public Equipment(Dictionary<string, object> input) {
            data = (Dictionary<string, int>)input["data"];

            slots[(int)SlotNames.Head] = (string)input["head"];
            slots[(int)SlotNames.Chest] = (string)input["chest"];
            slots[(int)SlotNames.RHand] = (string)input["rhand"];
            slots[(int)SlotNames.LHand] = (string)input["lhand"];
            slots[(int)SlotNames.Legs] = (string)input["legs"];
            slots[(int)SlotNames.Boots] = (string)input["boots"];
        }

        public void addItem(string name, int amount) {
            if (data.ContainsKey(name)) {
                data[name] += amount;
            } else {
                data.Add(name, amount);
            }
        }
        public void removeItem(string name, int amount) {
            // By allowing the user to remove below 0, we can "take all"
            // by taking a very large number of an item
            if (data.ContainsKey(name)) {
                data[name] -= amount;
                if (data[name] < 1) {
                    data.Remove(name);
                }
            } else {
                throw new Exception("Cannot remove item. It does not exist");
            }
        }
        
        public Item Head {
            get {
                if (slots[(int)SlotNames.Head] == "")
                    return ItemMngr.getItem(slots[(int)SlotNames.Head]);

                return new Item();
            }
            set {
                slots[(int)SlotNames.Head] = value.Name;
            }
        }
        public Item Chest {
            get {
                if (slots[(int)SlotNames.Chest] == "")
                    return ItemMngr.getItem(slots[(int)SlotNames.Chest]);

                return new Item();
            }
            set {
                slots[(int)SlotNames.Chest] = value.Name;
            }
        }
        public Item RHand {
            get {
                if (slots[(int)SlotNames.RHand] == "")
                    return ItemMngr.getItem(slots[(int)SlotNames.RHand]);

                return new Item();
            }
            set {
                slots[(int)SlotNames.RHand] = value.Name;
            }
        }
        public Item LHand {
            get {
                if (slots[(int)SlotNames.LHand] == "")
                    return ItemMngr.getItem(slots[(int)SlotNames.LHand]);

                return new Item();
            }
            set {
                slots[(int)SlotNames.LHand] = value.Name;
            }
        }
        public Item Legs {
            get {
                if (slots[(int)SlotNames.Legs] == "")
                    return ItemMngr.getItem(slots[(int)SlotNames.Legs]);

                return new Item();
            }
            set {
                slots[(int)SlotNames.Legs] = value.Name;
            }
        }
        public Item Boots {
            get {
                if (slots[(int)SlotNames.Boots] != "")
                    return ItemMngr.getItem(slots[(int)SlotNames.Boots]);

                return new Item();
            }
            set {
                slots[(int)SlotNames.Boots] = value.Name;
            }
        }

        public int bonusHealth() {
            return Head.HealthBonus + Chest.HealthBonus + RHand.HealthBonus +
                LHand.HealthBonus + Legs.HealthBonus + Boots.HealthBonus;
        }
        public int bonusMagic() {
            return Head.MagicBonus + Chest.MagicBonus + RHand.MagicBonus +
                LHand.MagicBonus + Legs.MagicBonus + Boots.MagicBonus;
        }
        public int bonusBody() {
            return Head.BodyBonus + Chest.BodyBonus + RHand.BodyBonus +
                LHand.BodyBonus + Legs.BodyBonus + Boots.BodyBonus;
        }
        public int bonusMind() {
            return Head.MindBonus + Chest.MindBonus + RHand.MindBonus +
                LHand.MindBonus + Legs.MindBonus + Boots.MindBonus;
        }
        public int bonusSpirit() {
            return Head.SpiritBonus + Chest.SpiritBonus + RHand.SpiritBonus +
                LHand.SpiritBonus + Legs.SpiritBonus + Boots.SpiritBonus;
        }
        public int bonusSocial() {
            return Head.SocialBonus + Chest.SocialBonus + RHand.SocialBonus +
                LHand.SocialBonus + Legs.SocialBonus + Boots.SocialBonus;
        }
        public int bonusSpeed() {
            return Head.SpeedBonus + Chest.SpeedBonus + RHand.SpeedBonus +
                LHand.SpeedBonus + Legs.SpeedBonus + Boots.SpeedBonus;
        }

        public Dictionary<string, object> ToJSON() {
            Dictionary<string, object> output = new Dictionary<string,object>();
            output.Add("data", data);
            output.Add("head", Head.Name);
            output.Add("chest", Chest.Name);
            output.Add("rhand", RHand.Name);
            output.Add("lhand", LHand.Name);
            output.Add("legs", Legs.Name);
            output.Add("boots", Boots.Name);

            return output;
        }
    }
}